-- local keymap = vim.keymap
-- keymap.set("n", "<leader>e", "<cmd>NvimTreeToggle<CR>")
-- local map = vim.api.nvim_set_keymap
-- map('模式','按键','映射为XX',opt)

-- <Leader><S-b> = leader + Shift + b

-- vim.api.nvim_set_keymap() 设置全局快捷键
-- vim.api.nvim_buf_set_keymap() 设置Buffer快捷键

local keymap = vim.keymap
local map = vim.api.nvim_set_keymap

-- 视图/插入模式下不需要映射上下左右方向键
keymap.set({ "n", "i" }, "<Left>", "<Nop>")
keymap.set({ "n", "i" }, "<Right>", "<Nop>")
keymap.set({ "n", "i" }, "<Up>", "<Nop>")
keymap.set({ "n", "i" }, "<Down>", "<Nop>")

-- vim 的原生文档树
-- keymap.set('', '<leader>e', ':Ex<CR>', { noremap = true })
-- keymap.set('', '<leader>e', '<cmd>Lex 30<cr>')
-- 使用 nvim-tree 替代 netrwPlugin

-- 取消掉默认的快捷键功能
keymap.set("n", "s", "<Nop>")
-- jk 替代 Esc
map("i", "jk", "<Esc>", { noremap = true, silent = true })

-- 取消掉默认的快捷键x的剪切到寄存器的功能
keymap.set("n", "x", '"_x', { noremap = true, silent = true })

-- 当超过编辑器的边界视觉上换行了，其实还是一行时，跳行的以实际算一行计算
-- https://www.reddit.com/r/vim/comments/2k4cbr/problem_with_gj_and_gk/
keymap.set("n", "j", [[v:count ? 'j' : 'gj']], { noremap = true, expr = true })
keymap.set("n", "k", [[v:count ? 'k' : 'gk']], { noremap = true, expr = true })


-- 修改单词
-- ciw         " 修改单词
keymap.set("n", "<space>i", "ciw")
-- caw         " 修改当前单词及后面的空格
keymap.set("n", "<space>I", "caw")

-- 上下移动光标 shift + jklh
keymap.set("n", "<S-j>", "4j")
keymap.set("n", "<S-k>", "4k")
keymap.set("n", "<S-l>", "4l")
keymap.set("n", "<S-h>", "4h")
-- Ctrl + ud 只移动10行，默认移动半屏
keymap.set("n", "<C-u>", "10k")
keymap.set("n", "<C-d>", "10j")

-- 数字加减
keymap.set("n", "<leader>+", "<C-a>")
keymap.set("n", "<leader>-", "<C-x>")

-- 写入（保存）
-- keymap.set('i', '<space>w', '<cmd>write<cr>', {desc = 'Save'})
keymap.set({ "n", "i" }, "<leader>w", "<cmd>write<cr>")

-- ctrl+s save
keymap.set("n", "<C-s>", ":w<CR>", { noremap = true, silent = true })
keymap.set("i", "<C-s>", "<ESC>:w<CR>", { noremap = true, silent = true })

-- Configure Copy Shortcuts
keymap.set("v", "<C-c>", '"+y', { noremap = true, silent = true })       -- copy
keymap.set("v", "<C-x>", '"+d', { noremap = true, silent = true })       -- cut
-- map("n", "<C-v>", '"+p', { noremap = true, silent = true }) -- paste from system clipboard
keymap.set("i", "<C-v>", '<ESC>"+pa', { noremap = true, silent = true }) -- paste from system clipboard

-- 关闭当前tab (close)
-- keymap.set('n', 'sc', '<C-w>c', { noremap = true, silent = true })
keymap.set("n", "<leader>sc", ":bdelete<CR>", { noremap = true, silent = true })
-- 关闭分割的其他tab (close other)
keymap.set("n", "<leader>so", "<C-w>o", { noremap = true, silent = true })

-- 关闭 split 的窗口
keymap.set("n", "<leader>sx", ":close<CR>", { noremap = true, silent = true })

-- keymap.set("n", "ts", ":tabn<CR>")
-- keymap.set("n", "tp", ":tabp<CR>")
-- keymap.set("n", "tn", ":tabnew<CR>")
-- keymap.set("n", "tc", ":tabclose<CR>")

-- 导航栏切换
keymap.set("n", "<A-Tab>", "<cmd>bNext<CR>", { silent = true })
-- 关闭 tab
keymap.set("n", "<leader>bd", "<cmd>bd<CR>")

-- 关闭所有的窗口 (close all)
keymap.set("n", "<leader>sa", "<C-w>o:q<CR>")
-- 关闭当前的窗口 (close current)
-- keymap.set("n", "<leader>q", ":q<CR>")
-- 强制关闭当前的窗口不保存 (close current)
keymap.set("n", "<leader>qq", ":q!<CR>")
keymap.set("n", "<leader>Q", ":qa!<CR>")

-- 取消高亮
keymap.set("n", "<leader>nh", ":nohl<CR>")

-- 光标回到行首
keymap.set("n", "H", "^") -- keymap.set("n", "H", "0")
-- 光标回到行未
keymap.set("n", "L", "$")
-- 向后删除
keymap.set("n", "<S-d>", "d$")
-- 向前删除
keymap.set("n", "<A-d>", "d0")

-- 删除： dt字符 删除当前光标到指定字符之间的内容

-- 打开一个终端
keymap.set("n", "<C-t><C-k>", ":split | :lcd %:p:h | terminal<CR>", { noremap = true, silent = true })
-- keymap.set("n", "<C-t><C-k>", ":vsplit | :lcd %:p:h | terminal<CR>")
-- -- Terminal related
-- map("n", "<leader>t", ":sp | terminal<CR>", { noremap = true })
-- map("n", "<leader>vt", ":vsp | terminal<CR>", { noremap = true })

-- vertical split 垂直
map("n", "<leader>ss", ":split<Return><C-w>w", { noremap = true, silent = true })
-- horizontal split 水平
map("n", "<leader>sv", ":vsplit<Return><C-w>w", { noremap = true, silent = true })

-- <C-w>v <C-w>s <C-w>= 分割
-- ：close<CR> 关闭分割的

-- pacman -S lazygit
-- -- 映射 alt + g 快捷键以打开 lazygit
map("n", "<M-g>", ":new<CR>:term lazygit<CR>i", { noremap = true, silent = true })

-- Delete a word forward
map("n", "de", 'vb"_d', { noremap = true, silent = true })
-- Delete a word backwards
map("n", "dw", 've"_d', { noremap = true, silent = true })

-- Select all
map("n", "<C-a>", "gg<S-v>G", { noremap = true, silent = true })

-- 单行或多行移动
-- M 表示 Alt 键。Up，Down 是方向键上下
map("v", "<M-Down>", ":m '>+1<CR>gv=gv", { noremap = true, silent = true })
map("v", "<M-Up>", ":m '<-2<CR>gv=gv", { noremap = true, silent = true })
-- Move selected text up and down
-- map("v", "J", ":move '>+1<CR>gv-gv", { noremap = true, silent = true })
-- map("v", "K", ":move '<-2<CR>gv-gv", { noremap = true, silent = true })

-- 连续 > 缩进代码
map("v", "<", "<gv", { noremap = true, silent = true })
map("v", ">", ">gv", { noremap = true, silent = true })


-- 在所有缓冲区(buffers)之间循环
map("n", "<PageUp>", ":bprevious<CR>", { noremap = true, silent = true })
map("n", "<PageDown>", ":bnext<CR>", { noremap = true, silent = true })

-- bufferline.nvim
-- bufferline 与 nvim-tree 之间的切换, 也就是顶部文件之间的切换
-- keymap.set("n", "<leader>l", ":bnext<CR>")
-- keymap.set("n", "<leader>h", ":bprevious<CR>")

-- buffer 的选择
keymap.set("n", "<leader>bb", ":buffers<cr>:b<space>")

-- 显示已经打开的tabs（buffers）,并选择进入的 buffers进行编辑
-- vim.keymap.set("n", "<leader>bb", function()
--   local last = vim.fn.getbufinfo("#")[1]
--   return ":buffers<CR>:b<Space>" .. (last ~= nil and last.bufnr or "")
-- end, {
--   noremap = true,
--   expr = true,
-- })
-- vim.cmd([[
--   nnoremap <Leader>bb :BB<CR>
--   command! BB call Buffers()

--   function! Buffers()
--     execute "buffers"
--     let buffer=input("Enter a buffer:")
--     execute "b" . buffer
--   endfunction
-- ]])
